class Solution:

    def smallestEquivalentString(self, s1: str, s2: str, baseStr: str) -> str:

        class UnionFind():

            def __init__(self, size: int) -> None:
                self.p = [i for i in range(size)]
                self.cnts = [1] * size
                self.size = size
                self.mx = [0]

            def find(self, a):
                if self.p[a] != a:
                    self.p[a] = self.find(self.p[a])
                return self.p[a]

            def union(self, a: int, b: int):
                A, B = self.find(a), self.find(b)
                if A != B:
                    x, y = min(A, B), max(A, B)
                    self.p[y] = x
                    self.cnts[x] += self.cnts[y]
                    self.cnts[y] = 0
                    self.size -= 1

            def isSameSet(self, a: int, b: int) -> bool:
                return self.find(a) == self.find(b)

        uf = UnionFind(128)
        for a, b in zip(s1, s2):
            uf.union(ord(a), ord(b))
        return "".join([chr(uf.find(ord(v))) for v in baseStr])